package kr.pe.johnkim.chattingday.mobile.rest.dao;

import kr.pe.johnkim.chattingday.mobile.domain.ChattingDayGatheringJoiner;
import kr.pe.johnkim.chattingday.mobile.domain.ChattingDayMember;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import java.util.List;

/**
 * Chatting Day 채팅방 DAO 메소드들을 구현하는 클래스 이다.
 * 
 * @author : johnkim <cale9797@gmail.com / Bettersoft Inc,.>
 * @Date : 12. 6. 7
 */
public class ChattingDayGatheringJoinerDaoImpl extends SqlMapClientDaoSupport implements ChattingDayGatheringJoinerDao {

    // SLF4J Logging
	private Logger logger = LoggerFactory.getLogger(ChattingDayGatheringJoinerDaoImpl.class);

    /**
     * 채팅방에 참여 한다.
     *
     * @param chattingDayGatheringJoiner
     * @return
     */
    @Override
    public boolean registerGatheringJoiner(ChattingDayGatheringJoiner chattingDayGatheringJoiner) {
        logger.info("registerGatheringJoiner");

        boolean result;

        try {
            getSqlMapClientTemplate().insert("CHATTINGDAY_GATHERING_JOINERS.registerGatheringJoiner", chattingDayGatheringJoiner);
            result = true;
        } catch (Exception e) {
            result = false;
        }

        return result;
    }

    /**
     * 채팅방에서 나온다.
     * <p/>
     * 나 자신을 채팅방에서 비활성화 처리 한다.
     *
     * @param chattingDayGatheringJoiner
     * @return
     */
    @Override
    public boolean updateDeactivateGatheringJoiner(ChattingDayGatheringJoiner chattingDayGatheringJoiner) {
        logger.info("updateDeactivateGatheringJoiner");

        boolean result;

        try {
            getSqlMapClientTemplate().update("CHATTINGDAY_GATHERING_JOINERS.updateDeactivateGatheringJoiner", chattingDayGatheringJoiner);
            result = true;
        } catch (Exception e) {
            result = false;
        }

        return result;
    }

    /**
     * 채팅방의 모든 사용자들을 비활성와 처리 한다.
     *
     * @param chattingDayGatheringJoiner
     * @return
     */
    @Override
    public boolean updateDeactivateAllGatheringJoiner(ChattingDayGatheringJoiner chattingDayGatheringJoiner) {
        logger.info("updateDeactivateAllGatheringJoiner");

        boolean result;

        try {
            getSqlMapClientTemplate().update("CHATTINGDAY_GATHERING_JOINERS.updateDeactivateAllGatheringJoiner", chattingDayGatheringJoiner);
            result = true;
        } catch (Exception e) {
            result = false;
        }

        return result;
    }

    /**
     * C2DM 메시지 발송을 위해서 채팅방 참여자들의 REGISTRATION ID 목록을 가져 온다.
     *
     * @param chattingDayGatheringJoiner
     * @return
     */
    @Override
    public List<ChattingDayMember> selectRegistrationIdList(ChattingDayGatheringJoiner chattingDayGatheringJoiner) {
        logger.info("selectRegistrationIdList");

        return this.getSqlMapClientTemplate().queryForList("CHATTINGDAY_GATHERING_JOINERS.selectRegistrationIdList", chattingDayGatheringJoiner);
    }
}
